<!DOCTYPE html>
<html lang="en-us">
  <head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="robots" content="noodp"/>
  <meta name="author" content="Mr.wei">
  
  
  
  <link rel="prev" href="http://blog.mr-wei.cn/2019/%E7%BC%96%E8%AF%91openwrt-18.06/" />
  <link rel="next" href="http://blog.mr-wei.cn/2019/16%E8%BF%9B%E5%88%B6%E7%9A%84%E5%86%85%E5%AD%98%E8%A1%A8%E7%A4%BA/" />
  <link rel="canonical" href="http://blog.mr-wei.cn/2019/openwrt%E4%B8%8A%E7%9A%84hello-world/" />
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  <link rel="manifest" href="/site.webmanifest">
  <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="theme-color" content="#ffffff">
  <title>
       
       
           OpenWrt上的Hello World | Mr.wei
       
  </title>
  <meta name="title" content="OpenWrt上的Hello World | Mr.wei">
    
  
  <link rel="stylesheet" href="/font/iconfont.css">
  <link rel="stylesheet" href="/css/main.min.css">


  
  
 

<script type="application/ld+json">
 "@context" : "http://schema.org",
    "@type" : "BlogPosting",
    "mainEntityOfPage": {
         "@type": "WebPage",
         "@id": "http:\/\/blog.mr-wei.cn"
    },
    "articleSection" : "posts",
    "name" : "OpenWrt上的Hello World",
    "headline" : "OpenWrt上的Hello World",
    "description" : "方法一 进入源码目录 cd ~\/openwrt  创建mypackages文件夹 mkdir mypackages  创建HelloWorld程序 mkdir -p mypackages\/HelloWorld\/src vim mypackages\/HelloWorld\/src\/HelloWorld.c  HelloWorld.c内容如下\n#include \x26lt;stdio.h\x26gt; int main(void){ printf(\x26quot;Hello,World\\n\x26quot;); return 0; }  创建Makefile文件 vim mypackages\/HelloWorld\/Makefile  Makefile内容如下\ninclude $(TOPDIR)\/rules.mk # Name, version and release number # The name and version of your package are used to define the variable to point to the build directory of your package: $(PKG_BUILD_DIR) PKG_NAME:=HelloWorld PKG_VERSION:=1.0 PKG_RELEASE:=1 # Source settings (i.",
    "inLanguage" : "en-us",
    "author" : "Mr.wei",
    "creator" : "Mr.wei",
    "publisher": "Mr.wei",
    "accountablePerson" : "Mr.wei",
    "copyrightHolder" : "Mr.wei",
    "copyrightYear" : "2019",
    "datePublished": "2019-09-03 09:28:14 \x2b0800 CST",
    "dateModified" : "2019-09-03 09:28:14 \x2b0800 CST",
    "url" : "http:\/\/blog.mr-wei.cn\/2019\/openwrt%E4%B8%8A%E7%9A%84hello-world\/",
    "wordCount" : "611",
    "keywords" : [ "OpenWrt", "Mr.wei"]
}
</script>

</head>

  


  <body class="">
    <div class="wrapper">
        <nav class="navbar">
    <div class="container">
        <div class="navbar-header header-logo">
        	<a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a>
        </div>
        <div class="menu navbar-right">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>
<nav class="navbar-mobile" id="nav-mobile" style="display: none">
     <div class="container">
        <div class="navbar-header">
            <div>  <a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a></div>
            <div class="menu-toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
     
          <div class="menu" id="mobile-menu">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>







    	 <main class="main">
          <div class="container">
      		
<article class="post-warp" itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
        <h1 class="post-title" itemprop="name headline">OpenWrt上的Hello World</h1>
        <div class="post-meta">
                Written by <a itemprop="name" href="http://blog.mr-wei.cn" rel="author">Mr.wei</a> with ♥ 
                <span class="post-time">
                on <time datetime=2019-09-03 itemprop="datePublished">September 3, 2019</time>
                </span>
                in
                <i class="iconfont icon-folder"></i>
                <span class="post-category">
                        
                </span>
        </div>
    </header>
    <div class="post-content">
        

        
            
        

        
        
     
          
          
          

          
          
          

          

<h1 id="方法一">方法一</h1>

<h2 id="进入源码目录">进入源码目录</h2>

<pre><code class="language-shell">cd ~/openwrt
</code></pre>

<h2 id="创建mypackages文件夹">创建mypackages文件夹</h2>

<pre><code class="language-shell">mkdir mypackages
</code></pre>

<h2 id="创建helloworld程序">创建HelloWorld程序</h2>

<pre><code class="language-shell">mkdir -p mypackages/HelloWorld/src
vim  mypackages/HelloWorld/src/HelloWorld.c
</code></pre>

<p>HelloWorld.c内容如下</p>

<pre><code class="language-c">#include &lt;stdio.h&gt;
int main(void){
	printf(&quot;Hello,World\n&quot;);
	return 0;
}
</code></pre>

<h2 id="创建makefile文件">创建Makefile文件</h2>

<pre><code class="language-shell">vim mypackages/HelloWorld/Makefile
</code></pre>

<p>Makefile内容如下</p>

<pre><code class="language-makefile">include $(TOPDIR)/rules.mk

# Name, version and release number
# The name and version of your package are used to define the variable to point to the build directory of your package: $(PKG_BUILD_DIR)
PKG_NAME:=HelloWorld
PKG_VERSION:=1.0
PKG_RELEASE:=1

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below
SOURCE_DIR:=./src

include $(INCLUDE_DIR)/package.mk

# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/HelloWorld
  SECTION:=mypackage
  CATEGORY:=MyPackage
  TITLE:=Hello, World!
endef

# Package description; a more verbose description on what our package does
define Package/HelloWorld/description
  A simple &quot;Hello, world!&quot; -application.
endef

# Package preparation instructions; create the build directory and copy the source code. 
# The last command is necessary to ensure our preparation instructions remain compatible with the patching system.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR)
	$(Build/Patch)
endef

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
#define Build/Compile
#	$(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/HelloWorld.o -c $(PKG_BUILD_DIR)/HelloWorld.c
#	$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/HelloWorld.o
#endef

# Package build instructions; invoke the GNU make tool to build our package
define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR) \
		CC=&quot;$(TARGET_CC)&quot; \
		CFLAGS=&quot;$(TARGET_CFLAGS)&quot; \
		LDFLAGS=&quot;$(TARGET_LDFLAGS)&quot;
endef

# Package install instructions; create a directory inside the package to hold our executable, and then copy the executable we built previously into the folder
define Package/HelloWorld/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/HelloWorld $(1)/usr/bin
endef

# This command is always the last, it uses the definitions and variables we give above in order to get the job done
$(eval $(call BuildPackage,HelloWorld))
</code></pre>

<p>创建子Makefile</p>

<pre><code class="language-shell">vim mypackages/HelloWorld/src/Makefile
</code></pre>

<p>Makefile内容如下</p>

<pre><code class="language-makefile"># Global target; when 'make' is run without arguments, this is what it should do
all: HelloWorld

# These variables hold the name of the compilation tool, the compilation flags and the link flags
# We make use of these variables in the package manifest
CC = gcc
CFLAGS = -Wall
LDFLAGS = 

# This variable identifies all header files in the directory; we use it to create a dependency chain between the object files and the source files
# This approach will re-build your application whenever any header file changes. In a more complex application, such behavior is often undesirable
DEPS = $(wildcard *.h)

# This variable holds all source files to consider for the build; we use a wildcard to pick all files
SRC = $(wildcard *.c)

# This variable holds all object file names, constructed from the source file names using pattern substitution
OBJ = $(patsubst %.c, %.o, $(SRC))

# This rule builds individual object files, and depends on the corresponding C source files and the header files
%.o: %.c $(DEPS)
	$(CC) -c -o $@ $&lt; $(CFLAGS)

# To build 'HelloWorld', we depend on the object files, and link them all into a single executable using the compilation tool
# We use automatic variables to specify the final executable name 'HelloWorld', using '$@' and the '$^' will hold the names of all the
# dependencies of this rule
HelloWorld: $(OBJ)
	$(CC) -o $@ $^ $(LDFLAGS)

# To clean build artifacts, we specify a 'clean' rule, and use PHONY to indicate that this rule never matches with a potential file in the directory
.PHONY: clean

clean:
	rm -f HelloWorld *.o
</code></pre>

<h2 id="创建feeds-conf">创建feeds.conf</h2>

<pre><code class="language-shell">vim feeds.conf
</code></pre>

<p>feeds.config内容如下</p>

<pre><code class="language-perl">src-link mypackages  /home/mr-wei/openwrt/mypackages
</code></pre>

<h2 id="更新并安装feeds">更新并安装feeds</h2>

<pre><code class="language-shell">./scripts/feeds update mypackages
./scripts/feeds install -a -p mypackages
</code></pre>

<h2 id="在配置界面中选择helloworld项">在配置界面中选择HelloWorld项</h2>

<pre><code class="language-shell">make menuconfig
</code></pre>

<p><figure><img src="/images/ring.svg" data-sizes="auto" data-src="http://pan.mr-wei.cn/image/Blog_image/OpenWrt%E4%B8%8A%E7%9A%84HelloWorld/makemenuconfig.png" alt="" class="lazyload"><figcaption class="image-caption"></figcaption></figure></p>

<h2 id="编译">编译</h2>

<pre><code class="language-shell">make V=s	#编译整个工程
make package/HelloWorld/compile		#单独编译HelloWorld程序
</code></pre>

<p>编译出的HelloWorld应用程序位于 ~/openwrt/bin/packages/mipsel_24kc/mypackages目录下</p>

<h1 id="方法二">方法二</h1>

<ol>
<li>在 ~/openwrt/package目录下创建HelloWorld文件夹及其子文件夹src</li>
<li>在HelloWorld文件夹下创建主Makefile文件内容同方法一的主Makefile</li>
<li>在src文件夹下创建子Makefile文件，内容同方法一子Makefile</li>
<li>在src文件夹下创建HelloWorld.c文件，内容同方法一中的HelloWorld.c</li>
<li>在配置界面选中HelloWorld选项</li>
<li>执行make V=s 编译</li>
<li>编译出的HelloWorld应用程序位于 ~/openwrt/bin/packages/mipsel_24kc/base目录下</li>
</ol>

<h1 id="参考资料">参考资料</h1>

<ul>
<li><font size=6><a href="https://openwrt.org/docs/guide-developer/helloworld/start" rel="nofollow noreferrer" target="_blank">&ldquo;Hello,World!&ldquo;for OpenWrt</a></font></li>
</ul>

    </div>

    <div class="post-copyright">
             
            <p class="copyright-item">
                <span>Author:</span>
                <span>Mr.wei </span>
                </p>
            
           
             
            <p class="copyright-item">
                    <span>Link:</span>
                    <a href=http://blog.mr-wei.cn/2019/openwrt%E4%B8%8A%E7%9A%84hello-world/>http://blog.mr-wei.cn/2019/openwrt%E4%B8%8A%E7%9A%84hello-world/</span>
            </p>
            
             
            <p class="copyright-item lincese">
                本文采用<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" #target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可
            </p>
            
    </div>

  
    <div class="post-tags">
        
            <section>
            <i class="iconfont icon-tag"></i>Tag(s): 
            
            <span class="tag"><a href="http://blog.mr-wei.cn/tags/openwrt/">
                    #OpenWrt</a></span>
            
            </section>
        
        <section>
                <a href="javascript:window.history.back();">back</a></span> · 
                <span><a href="http://blog.mr-wei.cn">home</a></span>
        </section>
    </div>

    <div class="post-nav">
        
        <a href="http://blog.mr-wei.cn/2019/%E7%BC%96%E8%AF%91openwrt-18.06/" class="prev" rel="prev" title="编译OpenWrt 18"><i class="iconfont icon-left"></i>&nbsp;编译OpenWrt 18</a>
         
        
        <a href="http://blog.mr-wei.cn/2019/16%E8%BF%9B%E5%88%B6%E7%9A%84%E5%86%85%E5%AD%98%E8%A1%A8%E7%A4%BA/" class="next" rel="next" title="16进制的内存表示">16进制的内存表示&nbsp;<i class="iconfont icon-right"></i></a>
        
    </div>

    <div class="post-comment">
          
                 
          
    </div>
</article>
          </div>
		   </main>
      <footer class="footer">
    <div class="copyright">
        &copy;
        
        <span itemprop="copyrightYear">2018 - 2019</span>
        
        <span class="with-love">
    	 <i class="iconfont icon-love"></i> 
         </span>
         
            <span class="author" itemprop="copyrightHolder"><a href="http://blog.mr-wei.cn">Mr.wei</a> | </span> 
         

         
            <a href="http://www.miibeian.gov.cn/" target="_blank" rel="external nofollow">冀ICP备 - 18032379号 </a> |
         
		  <span>Powered by <a href="https://gohugo.io/" target="_blank" rel="external nofollow">Hugo</a> & <a href="https://github.com/liuzc/leaveit" target="_blank" rel="external nofollow">LeaveIt</a></span> 
    </div>

	
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\[','\]']],
            processEscapes: true,
            processEnvironments: true,
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            TeX: {
                equationNumbers: { autoNumber: "AMS" },
                extensions: ["AMSmath.js", "AMSsymbols.js"]
            }
        }
    });
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
	
	

</footer>












    
     <link href="//lib.baomitu.com/lightgallery/1.6.11/css/lightgallery.min.css" rel="stylesheet">  
      
     <script src="/js/vendor_gallery.min.js" async="" ></script>
    
  



     </div>
  </body>
</html>
